package xyz.lspace.newlrshk;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import de.robv.android.xposed.XposedBridge;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LHttp {

    public static String baseUrl = "http://192.168.31.211:8889/";
    // public static String baseUrl = "";
    public static String regName = "qixun";
    public static boolean reg = false;

    public static void sendMsg(String str) {
        HashMap<String, String> map = new HashMap<>();
        map.put("msg", str);
        post(baseUrl + "msg", map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }

    public static void sendServer(String info) {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", regName);
        map.put("info", info);
        post(baseUrl + "sendServer", map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }

    public static void post(String url, Map<String, String> map, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : map.keySet()) {
            builder.add(key, map.get(key));
        }
        FormBody body = builder.build();

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }


    public static String get(String url) {
        XposedBridge.log("网络请求开始:");

        url = baseUrl + url;
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .build();
        final Call call = okHttpClient.newCall(request);
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                String string = call.execute().body().string();
                return DesBase64Util.decode("qwer8080", string);
            }
        });
        new Thread(futureTask).start();
        try {
            return futureTask.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }


}
